﻿using Day22EF.Adapters;
using Day22EF.Adapters.Data;
using Day22EF.Data;
using Day22EF.Data.Models;
using Day22EF.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Day22EF.Controllers
{
    public class HomeController : Controller
    {
        private IHomeAdapter _adapter;

        public HomeController()
        {
            _adapter = new HomeDataAdapter();
        }

        public ActionResult Index()
        {
            List<HeroViewModel> models = _adapter.GetHeroListViewModels();
            return View(models);
        }
        //GET overload
        public ActionResult Create()
        {
            CreateHeroViewModel model = _adapter.GetCreateHeroViewModel();
            return View(model);
        }

        //POST overload
        [HttpPost]
        public ActionResult Create(CreateHeroViewModel model)
        {
            _adapter.CreateHero(model);
            return RedirectToAction("Index");
        }

        //GET overload
        public ActionResult Edit(string id)
        {
            EditHeroViewModel model = _adapter.GetEditHeroViewModel(id);
            return View(model);
        }

        [HttpPost]
        public ActionResult Edit(EditHeroViewModel model)
        {
            _adapter.EditHero(model);
            return RedirectToAction("Index");
        }


        public ActionResult About()
        {
            HeroViewModel model = new HeroViewModel
            {
                HeroName = "BatMan",
                PowerName = "Batarang"
            };

            using (HeroDbContext db = new HeroDbContext())
            {
                Day22EF.Data.Models.User user = new Data.Models.User
                {
                    UserName = model.HeroName
                };

                db.Users.Add(user);

                Power p = new Power
                {
                    Name = model.PowerName,
                    User = user,
                    PowerTypeId = 2
                };

                db.Powers.Add(p);

                AffiliationUser au = new AffiliationUser
                {
                    User = user,
                    AffiliationId = 1
                };

                db.AffiliationUsers.Add(au);

                db.SaveChanges();
            }

            return View();
        }

        public ActionResult Contact()
        {
            ViewBag.Message = "Your contact page.";

            return View();
        }
    }
}